//
//  ActivityIndicatorView.mm
//  softphone
//
//  Created by Jiri Kral on 7/25/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "ActivityIndicatorView.h"


@implementation ActivityIndicatorView

@synthesize padding=_padding;
@synthesize isTextOnRight=_isTextOnRight;

static CGFloat BORDER_WIDTH = 10;

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
- (id)initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
    return [self initWithActivityIndicatorStyle:style onFullScreen: FALSE];
}

// This is used for waiting indicator when: connnecting..., syncing... in main view: IpadContactListController.
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
- (id)initWithFrame:(CGRect) frame activityIndicatorStyle:(UIActivityIndicatorViewStyle)style
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
    _isFullscreen = FALSE;
    if (self = [super initWithFrame:frame])
	{
		self.padding = 5;
		self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6];
		self.layer.cornerRadius = 10.0f;
		self.layer.borderWidth = BORDER_WIDTH;
		self.layer.borderColor = [UIColor clearColor].CGColor;
		
		_indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
		//self.frame = CGRectMake(0,0,_indicatorView.bounds.size.width + 2*self.padding,_indicatorView.bounds.size.height + 2*self.padding);
		self.frame = frame;
		CGRect iFrame = _indicatorView.bounds;
		iFrame.origin.x += 50;
		iFrame.origin.y += self.padding;
		_indicatorView.frame = iFrame;
		_indicatorView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin
		|UIViewAutoresizingFlexibleTopMargin
		|UIViewAutoresizingFlexibleLeftMargin
		|UIViewAutoresizingFlexibleRightMargin;
		[self addSubview:_indicatorView];
	}
    return self;
}
- (id)initWithFrame:(CGRect) frame title:(NSString*)title activityIndicatorStyle:(UIActivityIndicatorViewStyle)style
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
    _isFullscreen = FALSE;
    if (self = [super initWithFrame:frame])
	{
		self.padding = 5;
		self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6];
		self.layer.cornerRadius = 10.0f;
		self.layer.borderWidth = BORDER_WIDTH;
		self.layer.borderColor = [UIColor clearColor].CGColor;
		
		_indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
		//self.frame = CGRectMake(0,0,_indicatorView.bounds.size.width + 2*self.padding,_indicatorView.bounds.size.height + 2*self.padding);
				
		CGRect iFrame = _indicatorView.bounds;
		iFrame.origin.x += 50;
		iFrame.origin.y += self.padding;
		_indicatorView.frame = iFrame;
		_indicatorView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin
		|UIViewAutoresizingFlexibleTopMargin
		|UIViewAutoresizingFlexibleLeftMargin
		|UIViewAutoresizingFlexibleRightMargin;
		[self addSubview:_indicatorView];
						
		//frame.size.width = _indicatorView.frame.origin.x + _indicatorView.frame.size.width + 5 + sz.width + _indicatorView.frame.origin.x;
		
		//self.frame = frame;
	}
    return self;
}

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
- (id)initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style onFullScreen: (BOOL) isFullScreen
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
	_isFullscreen = isFullScreen;
    if (self = [super initWithFrame:CGRectZero])
	{
		self.padding = 16;
		self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6];
		self.layer.cornerRadius = 10.0f;
		self.layer.borderWidth = 10;
		self.layer.borderColor = [UIColor clearColor].CGColor;
		
		_indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
		self.frame = CGRectMake(0,0,_indicatorView.bounds.size.width + 2*self.padding,_indicatorView.bounds.size.height + 2*self.padding);
		CGRect iFrame = _indicatorView.bounds;
		iFrame.origin.x += self.padding;
		iFrame.origin.y += self.padding;
		_indicatorView.frame = iFrame;
		_indicatorView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin
											|UIViewAutoresizingFlexibleTopMargin
											|UIViewAutoresizingFlexibleLeftMargin
											|UIViewAutoresizingFlexibleRightMargin;
		[self addSubview:_indicatorView];
	}
    return self;
}

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
-(void) setText: (NSString*) text
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
	if ([text compare:@"Determining..."] == NSOrderedSame)
	{
		CGRect iFrame = _indicatorView.frame;
		iFrame.origin.x = 5; 
		iFrame.size.width = 20;
		iFrame.size.height = 20;
		_indicatorView.frame = iFrame;
	}
	if (text.length > 0)
	{
		if (!_label) {
			CGRect f = self.bounds;
			
			f.size.height = 30;
			CGFloat xOffset = 0;
			CGFloat yOffset = 0;
			if(_isTextOnRight)
			{
				CGRect iFrame = _indicatorView.frame;
				xOffset = _indicatorView.frame.origin.x + _indicatorView.frame.size.width + 5;
				yOffset = (self.bounds.size.height - f.size.height)/2;
				f.size.width = self.bounds.size.width - xOffset;
			} else {
				xOffset = 3;
				yOffset = self.padding + _indicatorView.frame.size.height + 3;
			}
			
			f.origin.y = yOffset;
			if (_isFullscreen) {
				f.origin.y = _indicatorView.frame.origin.y + 5;
			}
			
			f.origin.x = xOffset;
			if (_isFullscreen) {
				f.origin.x = self.padding - 8;
			}
			f.size.width -= 6;
			_label = [[UILabel alloc] initWithFrame: f];
			if(_isTextOnRight)
			{
				_label.font = [UIFont systemFontOfSize:15];
			} else {
				_label.font = [UIFont systemFontOfSize:10];
			}
			
			_label.textColor = [UIColor whiteColor];
			if ([text compare:@"Determining..."] == NSOrderedSame)
				_label.textColor = [UIColor blackColor];
			_label.shadowColor = [UIColor grayColor];
			_label.shadowOffset = CGSizeMake(0,1);
			_label.backgroundColor = [UIColor clearColor];
			_label.textAlignment = UITextAlignmentCenter;
			_label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
			if (_isFullscreen) {
				_label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
			}
			_label.numberOfLines = 2;
			f = self.frame;
			if(_isTextOnRight)
			{				
				f.size.height += BORDER_WIDTH;
				
				CGRect iFrame = _indicatorView.frame;
				
				CGRect lableFrame = _label.frame;
				
				CGFloat leftPadding = (f.size.width - iFrame.size.width - lableFrame.size.width - 5)/2;
				iFrame.origin.y = (f.size.height - BORDER_WIDTH - iFrame.size.height)/2 - 1.5;
				if ([text compare:@"Determining..."] == NSOrderedSame)
				{
					iFrame.origin.x = 5; 
					lableFrame.origin.y -= 5; 
				}
				else
					iFrame.origin.x = leftPadding;
				lableFrame.origin.x = iFrame.origin.x + iFrame.size.width + 5;
				lableFrame.size.width = self.bounds.size.width - lableFrame.origin.x - leftPadding;
				_indicatorView.frame = iFrame;
				_label.frame = lableFrame;				
			} else {
				f.size.height += 30 + 6 - self.padding;
			}
			
			if (!_isFullscreen) {
				self.frame = f;
			}
			[self addSubview:_label];

		}
		_label.text = text;
	} else {
		[_label removeFromSuperview];
		[_label release];
		_label = nil;
		[self setNeedsLayout];
	}
	
}

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
-(NSString*) text
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
	return _label.text;
}

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
-(void) startAnimating
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
	[_indicatorView startAnimating];
	self.hidden = NO;
}

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
-(void) stopAnimating
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
	[_indicatorView stopAnimating];
	if(self.hidesWhenStopped)
		self.hidden = YES;
}

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
-(void) setHidesWhenStopped:(BOOL) b
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
	[_indicatorView setHidesWhenStopped:b];
}

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
-(BOOL) hidesWhenStopped
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
	return [_indicatorView hidesWhenStopped];
}

- (BOOL) isAnimating
{
	return [_indicatorView isAnimating];
}

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
- (void)dealloc
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
	[_indicatorView release];
	[_label release];
    [super dealloc];
}

@end
